package org.jeecg.modules.flowable.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.flowable.bpmn.model.Process;
import org.flowable.bpmn.model.*;
import org.flowable.engine.*;
import org.flowable.engine.history.HistoricProcessInstance;
import org.flowable.engine.repository.ProcessDefinition;
import org.flowable.engine.repository.ProcessDefinitionQuery;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.image.ProcessDiagramGenerator;
import org.jeecg.modules.flowable.common.util.IdUtil;
import org.jeecg.modules.flowable.common.util.tree.TreeDesc;
import org.jeecg.modules.flowable.common.util.tree.TreeDescUtil;
import org.jeecg.modules.flowable.common.util.validation.AssertValid;
import org.jeecg.modules.flowable.common.util.validation.MustParamValidation;
import org.jeecg.modules.flowable.dic.TaskScopeType;
import org.jeecg.modules.flowable.entity.*;
import org.jeecg.modules.flowable.exception.ModuleFlowableException;
import org.jeecg.modules.flowable.mapper.*;
import org.jeecg.modules.flowable.service.FlowableService;
import org.jeecg.modules.flowable.service.ITActTaskConfActionService;
import org.jeecg.modules.flowable.service.ITActTaskConfExtService;
import org.jeecg.modules.flowable.service.ITActTaskScopeService;
import org.jeecg.modules.flowable.service.bo.TaskConfList;
import org.jeecg.modules.flowable.service.bo.taskconf.ExtParam;
import org.jeecg.modules.flowable.service.bo.taskconf.TaskConf;
import org.jeecg.modules.flowable.service.bo.taskconf.TaskConfResult;
import org.jeecg.modules.flowable.system.core.SystemUser;
import org.jeecg.modules.flowable.system.etc.SystemUserUtil;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Title:  <br>
 * Desc:  <br>
 * Date: 6/8/22 2:06 PM <br>
 * Company: 联通（辽宁）产业互联网有限公司 <br>
 *
 * @author zxd
 * @version 1.0.0
 */
@Service
@Slf4j
public class FlowableServiceImpl implements FlowableService {
    @Autowired
    private HistoryService historyService ;
    @Autowired
    private RuntimeService runtimeService;
    @Autowired
    private TaskService taskService;
    @Autowired
    private IdentityService identityService;
    @Autowired
    private RepositoryService repositoryService;
    @Autowired
    private ExtFlowableMapper extFlowableMapper;
    @Autowired
    private TActTaskScopeMapper tActTaskScopeMapper;
    @Autowired
    private ITActTaskScopeService iTActTaskScopeService;
    @Autowired
    private TActTaskConfExtMapper tActTaskConfExtMapper;
    @Autowired
    private ITActTaskConfExtService iTActTaskConfExtService;
    @Autowired
    private TActTaskConfMapper tActTaskConfMapper;
    @Autowired
    private TActTaskConfActionMapper tActTaskConfActionMapper;
    @Autowired
    private ITActTaskConfActionService iTActTaskConfActionService;
    @Autowired
    private TActActionMapper tActActionMapper;
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Autowired
    private TActRuTaskScopeMapper tActRuTaskScopeMapper;


    @Override
    public InputStream getFlowDesignerByProcessInstanceId(String argProcessInstanceId) {

        // 获得当前活动的节点
        String processDefinitionId=null;

        List<ProcessInstance> list = runtimeService.createProcessInstanceQuery().processInstanceId(argProcessInstanceId).list();
        if(AssertValid.isEmpty(list)) {
            List<HistoricProcessInstance> list1 = historyService.createHistoricProcessInstanceQuery().processInstanceId(argProcessInstanceId).list();
            if(AssertValid.isNotEmpty(list1)) {
                processDefinitionId=list1.get(0).getProcessDefinitionId();
            } else {
                throw new ModuleFlowableException("流程不存在");
            }
        } else {
            processDefinitionId=list.get(0).getProcessDefinitionId();
        }

        // 如果流程已经结束，则得到结束节点
        Boolean isFinish= this.isFlowFinish(argProcessInstanceId);
        // 获得所有流程模型
        BpmnModel bpmnModel = repositoryService.getBpmnModel(processDefinitionId);
        List<Process> processes = bpmnModel.getProcesses();
        Process process = processes.get(0);
        Collection<FlowElement> flowElements = process.getFlowElements();

        // 查询高亮
        List<String> highLightedActivities = new ArrayList<>();
        if(isFinish) {
            f:for (FlowElement flowElement : flowElements) {
                if(flowElements instanceof EndEvent) {
                    highLightedActivities.add(((EndEvent) flowElements).getId());
                    break f;
                }
            }
        } else {
            // 查询当前活动的任务
            List<Map<String, Object>> maps = jdbcTemplate.queryForList("select ACT_ID_ as ACT_ID,IS_ACTIVE_ as IS_ACTIVE from act_ru_execution where PROC_INST_ID_='"+argProcessInstanceId+"' AND ACT_ID_ is not null");
            // 判断有没有为0的数据
//            Boolean abc=false;
//            for (Map<String, Object> map : maps) {
//                if(map.get("IS_ACTIVE").toString().trim().equals("0")) {
//                    abc = true;
//                    highLightedActivities.add(map.get("ACT_ID").toString());
//                }
//            }
//            if(!abc) {
                for (Map<String, Object> map : maps) {
                    String act_id1 = map.get("ACT_ID").toString();
                    highLightedActivities.add(act_id1);
                }
//            }
        }
        // 获取流程图
        ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
        ProcessEngineConfiguration engConf = defaultProcessEngine.getProcessEngineConfiguration();

        ProcessDiagramGenerator diagramGenerator = engConf.getProcessDiagramGenerator();
        InputStream in = diagramGenerator.generateDiagram(bpmnModel, "png", highLightedActivities, highLightedActivities, "宋体",
                "宋体", "宋体", engConf.getClassLoader(), 1.0, true);
        return in;
    }

    @Override
    public InputStream getFlowDesignerByFlowKey(String argFlowKey) {

        ProcessDefinitionQuery processDefinitionQuery = repositoryService.createProcessDefinitionQuery().processDefinitionKey(argFlowKey).orderByProcessDefinitionVersion().desc();
        List<ProcessDefinition> list1 = processDefinitionQuery.list();
        // 获取流程图
        BpmnModel bpmnModel = repositoryService.getBpmnModel(list1.get(0).getId());
        ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
        ProcessEngineConfiguration engConf = defaultProcessEngine.getProcessEngineConfiguration();

        ProcessDiagramGenerator diagramGenerator = engConf.getProcessDiagramGenerator();
        InputStream in = diagramGenerator.generateDiagram(bpmnModel, "png", new ArrayList<>(), new ArrayList<>(), "宋体",
                "宋体", "宋体", engConf.getClassLoader(), 1.0, true);
        return in;
    }

    @Override
    public Boolean isFlowFinish(String argProcessInstanceId) {
        List<HistoricProcessInstance> list = historyService.createHistoricProcessInstanceQuery().processInstanceId(argProcessInstanceId).list();
        List<ProcessInstance> list1 = runtimeService.createProcessInstanceQuery().processInstanceId(argProcessInstanceId).list();

        if(list1!=null && list1.size()>0)
            return false;
        else {
            if(list!=null && list.size()>0)
                return true;
            else
                return false;
        }
    }

    @Override
    public List<TaskConfList> getTaskConfList(String argProcessDefinitionId) {

        // 查询所有用户任务
        List<TaskConfList> list = new ArrayList<>();
        ProcessDefinitionQuery processDefinitionQuery = repositoryService.createProcessDefinitionQuery().processDefinitionId(argProcessDefinitionId).orderByProcessDefinitionVersion().desc();
        List<ProcessDefinition> list1 = processDefinitionQuery.list();
        BpmnModel bpmnModel = repositoryService.getBpmnModel(list1.get(0).getId());
        List<Process> processes = bpmnModel.getProcesses();
        Process process = processes.get(0);
        Collection<FlowElement> flowElements = process.getFlowElements();
        for (FlowElement flowElement : flowElements) {
            if(flowElement instanceof UserTask) {
                TaskConfList taskConf = new TaskConfList();
                taskConf.setProcessDefinitionId(argProcessDefinitionId).setTaskId(flowElement.getId()).setTaskName(flowElement.getName());
                list.add(taskConf);
            }
        }
        // 查询每个节点配置的权限
        List<String> collect = list.stream().map(s -> {
            return s.getTaskId();
        }).collect(Collectors.toList());
        List<Map> maps = extFlowableMapper.queryTaskConf(collect);
        // 拼接LIST
        for (TaskConfList taskConf : list) {
            String taskId = taskConf.getTaskId();
            f:for (Map map : maps) {
                if(taskId.equals(map.get("TASK_DEF_KEY"))) {
                    // 角色名称赋值，相对角色赋值
                    taskConf.setRoleName(map.get("ROLE_NAMES")==null?"":map.get("ROLE_NAMES").toString());
                    if(map.get("IS_RELATIVE") !=null && map.get("IS_RELATIVE").toString().trim().equals("1"))
                        taskConf.setRelative(true);
                    else
                        taskConf.setRelative(false);
                    // 处理人姓名赋值
                    taskConf.setUserName(map.get("USER_NAMES")==null?"":map.get("USER_NAMES").toString());

                    taskConf.setUserIds(map.get("USER_IDS")==null?"":map.get("USER_IDS").toString());
                    taskConf.setRoleIds(map.get("ROLE_IDS")==null?"":map.get("ROLE_IDS").toString());
                    break f;
                }
            }
        }
        return list;
    }

    @Override
    public TreeDesc queryOrgTreeDesc() {
        List<Map<String, Object>> maps = extFlowableMapper.queryOrg();
        TreeDesc treeDesc = TreeDescUtil.getTreeDesc("", maps, "PARENT_ID", "ID", "DEPART_NAME", true,null);
        return treeDesc;
    }

    @Override
    public TreeDesc queryRegionTreeDesc() {
        List<Map<String, Object>> maps = extFlowableMapper.queryRegion();
        TreeDesc treeDesc = TreeDescUtil.getTreeDesc("", maps, "PID", "ID", "REGION_NAME", true,null);
        return treeDesc;
    }

    @Override
    public List<Map<String, Object>> queryUserList(String argUserName) {
        List<Map<String, Object>> maps = extFlowableMapper.queryUserList(argUserName);
        return maps;
    }

    @Override
    public List<Map<String, Object>> queryRoleList(String argRoleName) {
        List<Map<String, Object>> maps = extFlowableMapper.queryRoleList(argRoleName);
        return maps;
    }

    @Override
    @Transactional
    public TaskConf saveTaskConf(TaskConf argTaskConf) {

        MustParamValidation.mustValidation(argTaskConf.getTaskDefKey());
        SystemUser loginUser = SystemUserUtil.getSystemUser();
        DateTime now = DateTime.now();
        // 先删除权限范围数据
        LambdaQueryWrapper<TActTaskScopeEntity> queryWrapper = new LambdaQueryWrapper<TActTaskScopeEntity>();
        queryWrapper.eq(TActTaskScopeEntity::getTaskDefKey,argTaskConf.getTaskDefKey());
        tActTaskScopeMapper.delete(queryWrapper);
        // 删除扩展数据
        LambdaQueryWrapper<TActTaskConfExtEntity> queryWrapper2 = new LambdaQueryWrapper<TActTaskConfExtEntity>();
        queryWrapper2.eq(TActTaskConfExtEntity::getTaskDefKey,argTaskConf.getTaskDefKey());
        tActTaskConfExtMapper.delete(queryWrapper2);
        // 删除权限数据
        LambdaQueryWrapper<TActTaskConfEntity> queryWrapper3 = new LambdaQueryWrapper<TActTaskConfEntity>();
        queryWrapper3.eq(TActTaskConfEntity::getTaskDefKey,argTaskConf.getTaskDefKey());
        tActTaskConfMapper.delete(queryWrapper3);
        // 删除常用动作
        LambdaQueryWrapper<TActTaskConfActionEntity> queryWrapper4 = new LambdaQueryWrapper<TActTaskConfActionEntity>();
        queryWrapper4.eq(TActTaskConfActionEntity::getTaskDefKey,argTaskConf.getTaskDefKey());
        tActTaskConfActionMapper.delete(queryWrapper4);
        // 构建权限范围数据对像
        List<TActTaskScopeEntity> objects = new ArrayList<>();
        // 处理绑定角色数据
        String roleId1 = argTaskConf.getRoleId();
        if(AssertValid.isNotEmpty(roleId1)) {
            TActTaskScopeEntity tActTaskScopeEntity = new TActTaskScopeEntity().setId(IdUtil.getStringId()).setCreateBy(loginUser.getRealname()).setCreateTime(now.toDate()).setUpdateBy(loginUser.getRealname()).setUpdateTime(now.toDate()).setDelFlag(0).setTaskDefKey(argTaskConf.getTaskDefKey());
            tActTaskScopeEntity.setOptId(roleId1).setTaskScopeType(TaskScopeType.ROLE);
            if(argTaskConf.getRelative()!=null && argTaskConf.getRelative())
                tActTaskScopeEntity.setIsRelative(1);
            else
                tActTaskScopeEntity.setIsRelative(0);
            objects.add(tActTaskScopeEntity);
        }

        // 处理用户数据
        String userId=argTaskConf.getUserId();
        if(AssertValid.isNotEmpty(userId)) {
            TActTaskScopeEntity tActTaskScopeEntity = new TActTaskScopeEntity().setId(IdUtil.getStringId()).setCreateBy(loginUser.getRealname()).setCreateTime(now.toDate()).setUpdateBy(loginUser.getRealname()).setUpdateTime(now.toDate()).setDelFlag(0).setTaskDefKey(argTaskConf.getTaskDefKey()).setOptId(userId).setTaskScopeType(TaskScopeType.USER);
            objects.add(tActTaskScopeEntity);
        }
        // 处理扩展数据
        List<TActTaskConfExtEntity> objects1 = new ArrayList<>();
        List<ExtParam> extParam = argTaskConf.getExtParam();
        if(AssertValid.isNotEmpty(extParam)) {
            int i=0;
            for (ExtParam param : extParam) {
                String key = param.getKey();
                String value = param.getValue();
                String desc = param.getDesc();
                TActTaskConfExtEntity tActTaskConfExtEntity = new TActTaskConfExtEntity().setId(IdUtil.getStringId()).setCreateBy(loginUser.getRealname()).setCreateTime(now.toDate()).setUpdateBy(loginUser.getRealname()).setUpdateTime(now.toDate()).setDelFlag(0).setTaskDefKey(argTaskConf.getTaskDefKey()).setExtKey(key).setExtValue(value).setExtKeyDesc(desc).setInx(i++);
                objects1.add(tActTaskConfExtEntity);
            }
        }
        // 任务关联动作
        ArrayList<TActTaskConfActionEntity> objects2 = new ArrayList<>();
        int i=0;
        if(AssertValid.isNotEmpty(argTaskConf.getActionIds())) {
            List<String> actionIds = argTaskConf.getActionIds();
            for (String actionId : actionIds) {
                TActTaskConfActionEntity tActTaskConfActionEntity = new TActTaskConfActionEntity().setId(IdUtil.getStringId()).setCreateBy(loginUser.getRealname()).setCreateTime(now.toDate()).setUpdateBy(loginUser.getRealname()).setUpdateTime(now.toDate()).setDelFlag(0).setTaskDefKey(argTaskConf.getTaskDefKey());
                tActTaskConfActionEntity.setActActionId(actionId);
                tActTaskConfActionEntity.setTaskDefKey(argTaskConf.getTaskDefKey());
                tActTaskConfActionEntity.setInx(++i);
                objects2.add(tActTaskConfActionEntity);
            }
        }
        // 处理权限数据
        TActTaskConfEntity tActTaskConfEntity = new TActTaskConfEntity().setId(IdUtil.getStringId()).setCreateBy(loginUser.getRealname()).setCreateTime(now.toDate()).setUpdateBy(loginUser.getRealname()).setUpdateTime(now.toDate()).setDelFlag(0).setTaskDefKey(argTaskConf.getTaskDefKey());
        if(AssertValid.isNotEmpty(argTaskConf.getAttAgree()))
            tActTaskConfEntity.setAttAgree(argTaskConf.getAttAgree()?1:0);
        else
            tActTaskConfEntity.setAttAgree(0);
        if(AssertValid.isNotEmpty(argTaskConf.getAttDontagree()))
            tActTaskConfEntity.setAttDontagree(argTaskConf.getAttDontagree()?1:0);
        else
            tActTaskConfEntity.setAttDontagree(0);
        tActTaskConfEntity.setAttDontagreeConf(argTaskConf.getAttDontagreeConf());

        if(AssertValid.isNotEmpty(argTaskConf.getAttHaveread()))
            tActTaskConfEntity.setAttHaveread(argTaskConf.getAttHaveread()?1:0);
        else
            tActTaskConfEntity.setAttHaveread(0);
        tActTaskConfEntity.setAttDontagreeDefault(argTaskConf.getAttDontagreeDefault());

        tActTaskConfEntity.setFormKey(argTaskConf.getFormKey());
        if(AssertValid.isNotEmpty(argTaskConf.getYjDontagreeMust()))
            tActTaskConfEntity.setYjDontagreeMust(argTaskConf.getYjDontagreeMust()?1:0);
        else
            tActTaskConfEntity.setYjDontagreeMust(0);

        if(AssertValid.isNotEmpty(argTaskConf.getYjMust()))
            tActTaskConfEntity.setYjMust(argTaskConf.getYjMust()?1:0);
        else
            tActTaskConfEntity.setYjMust(0);

        if(AssertValid.isNotEmpty(argTaskConf.getYjUndoMust()))
            tActTaskConfEntity.setYjUndoMust(argTaskConf.getYjUndoMust()?1:0);
        else
            tActTaskConfEntity.setYjUndoMust(0);

        tActTaskConfEntity.setFormDesigner(argTaskConf.getFormDesigner());

        // 保存基础配置数据
        tActTaskConfMapper.insert(tActTaskConfEntity);
        // 批量保存权限数据
        iTActTaskScopeService.saveBatch(objects);
        // 批量保存扩展数据
        iTActTaskConfExtService.saveBatch(objects1);
        // 批量保存动作数据
        iTActTaskConfActionService.saveBatch(objects2);

        return argTaskConf;
    }

    @Override
    public TaskConfResult queryTaskConf(String taskDefKey) {
        MustParamValidation.mustValidation(taskDefKey);
        // 构建返回结果对像
        TaskConfResult taskConfResult = new TaskConfResult();
        taskConfResult.setTaskDefKey(taskDefKey);
        taskConfResult.setUserId("");
        // 查询权限范围数据
        LambdaQueryWrapper<TActTaskScopeEntity> queryWrapper = new LambdaQueryWrapper<TActTaskScopeEntity>();
        queryWrapper.eq(TActTaskScopeEntity::getTaskDefKey,taskDefKey);
        List<TActTaskScopeEntity> tActTaskScopeEntities = tActTaskScopeMapper.selectList(queryWrapper);
        for (TActTaskScopeEntity tActTaskScopeEntity : tActTaskScopeEntities) {
            if(tActTaskScopeEntity.getTaskScopeType().equals(TaskScopeType.ROLE)) {
                if(AssertValid.isNotEmpty(tActTaskScopeEntity.getOptId())) {
                    taskConfResult.setRoleId(tActTaskScopeEntity.getOptId());
                    if(tActTaskScopeEntity.getIsRelative()!=null && tActTaskScopeEntity.getIsRelative().intValue()==1)
                        taskConfResult.setRelative(true);
                    else
                        taskConfResult.setRelative(false);
                }
            } else if(tActTaskScopeEntity.getTaskScopeType().equals(TaskScopeType.USER)) {
                if(AssertValid.isNotEmpty(tActTaskScopeEntity.getOptId()))
                    taskConfResult.setUserId(tActTaskScopeEntity.getOptId());
            }
        }
        // 写入其它配置数据
        LambdaQueryWrapper<TActTaskConfEntity> queryWrapper2 = new LambdaQueryWrapper<TActTaskConfEntity>();
        queryWrapper2.eq(TActTaskConfEntity::getTaskDefKey,taskDefKey);
        List<TActTaskConfEntity> tActTaskConfEntities = tActTaskConfMapper.selectList(queryWrapper2);
        if(AssertValid.isNotEmpty(tActTaskConfEntities)) {
            TActTaskConfEntity tActTaskConfEntity = tActTaskConfEntities.get(0);
            if(AssertValid.isNotEmpty(tActTaskConfEntity.getAttAgree()))
                taskConfResult.setAttAgree(tActTaskConfEntity.getAttAgree().intValue()==0?false:true);
            else
                taskConfResult.setAttAgree(false);
            if(AssertValid.isNotEmpty(tActTaskConfEntity.getAttDontagree()))
                taskConfResult.setAttDontagree(tActTaskConfEntity.getAttDontagree().intValue()==0?false:true);
            else
                taskConfResult.setAttDontagree(false);
            if(AssertValid.isNotEmpty(tActTaskConfEntity.getAttDontagreeConf()))
                taskConfResult.setAttDontagreeConf(tActTaskConfEntity.getAttDontagreeConf());
            else
                taskConfResult.setAttDontagreeConf("");

            if(AssertValid.isNotEmpty(tActTaskConfEntity.getAttDontagreeDefault()))
                taskConfResult.setAttDontagreeDefault(tActTaskConfEntity.getAttDontagreeDefault());
            else
                taskConfResult.setAttDontagreeDefault("");
            if(AssertValid.isNotEmpty(tActTaskConfEntity.getAttHaveread()))
                taskConfResult.setAttHaveread(tActTaskConfEntity.getAttHaveread().intValue()==0?false:true);
            else
                taskConfResult.setAttHaveread(false);

            if(AssertValid.isNotEmpty(tActTaskConfEntity.getYjDontagreeMust()))
                taskConfResult.setYjDontagreeMust(tActTaskConfEntity.getYjDontagreeMust().intValue()==0?false:true);
            else
                taskConfResult.setYjDontagreeMust(false);

            if(AssertValid.isNotEmpty(tActTaskConfEntity.getYjMust())) {
                taskConfResult.setYjMust(tActTaskConfEntity.getYjMust().intValue()==0?false:true);
            } else {
                taskConfResult.setYjMust(false);
            }
            if(AssertValid.isNotEmpty(tActTaskConfEntity.getYjUndoMust())) {
                taskConfResult.setYjUndoMust(tActTaskConfEntity.getYjUndoMust()==0?false:true);
            } else {
                taskConfResult.setYjUndoMust(false);
            }
            taskConfResult.setFormKey(tActTaskConfEntity.getFormKey());
            taskConfResult.setFormDesigner(tActTaskConfEntity.getFormDesigner());
        }

        // 设置常用动作
        taskConfResult.setActionIds(new ArrayList<>());
        LambdaQueryWrapper<TActTaskConfActionEntity> queryWrapper3 = new LambdaQueryWrapper<TActTaskConfActionEntity>();
        queryWrapper3.eq(TActTaskConfActionEntity::getTaskDefKey,taskDefKey);
        queryWrapper3.orderByAsc(TActTaskConfActionEntity::getInx);
        List<TActTaskConfActionEntity> tActTaskConfActionEntities = tActTaskConfActionMapper.selectList(queryWrapper3);
        taskConfResult.setActionEntity(new ArrayList<>());
        for (TActTaskConfActionEntity tActTaskConfActionEntity : tActTaskConfActionEntities) {
            taskConfResult.getActionIds().add(tActTaskConfActionEntity.getActActionId());
            TActActionEntity tActActionEntity = tActActionMapper.selectById(tActTaskConfActionEntity.getActActionId());
            if(AssertValid.isNotEmpty(tActActionEntity)) {
                tActActionEntity.setExtBb(tActTaskConfActionEntity.getInx()==null?"-1":tActTaskConfActionEntity.getInx().toString());
                taskConfResult.getActionEntity().add(tActActionEntity);
            } else {
                // 如果它为空的情况说明动作被删除掉了，这种情况有可能是存在调整后台数据后遗留些脏数据，查的时候就不返回它啦 。
                continue;
            }
        }
        // 查询扩展配置数据
        LambdaQueryWrapper<TActTaskConfExtEntity> queryWrapper5 = new LambdaQueryWrapper<TActTaskConfExtEntity>();
        queryWrapper5.eq(TActTaskConfExtEntity::getTaskDefKey,taskDefKey);
        List<TActTaskConfExtEntity> tActTaskConfExtEntities = tActTaskConfExtMapper.selectList(queryWrapper5);
        List<ExtParam> collect = tActTaskConfExtEntities.stream().map(s -> {
            ExtParam extParam = new ExtParam().setDesc(s.getExtKeyDesc()).setKey(s.getExtKey()).setValue(s.getExtValue());
            return extParam;
        }).collect(Collectors.toList());
        taskConfResult.setExtParam(collect);
        // 角色
        if(AssertValid.isNotEmpty(taskConfResult.getRoleId())) {
            List<Map<String, Object>> maps = jdbcTemplate.queryForList("SELECT id as id,role_name as roleName FROM sys_role where ID='"+taskConfResult.getRoleId()+"'");
            if(AssertValid.isNotEmpty(maps)) {
                taskConfResult.setRole(maps.get(0));
            }
        }
        // 用户
        if(AssertValid.isNotEmpty(taskConfResult.getUserId())) {
            List<Map<String, Object>> maps = jdbcTemplate.queryForList("select id,realname from sys_user where id ='"+taskConfResult.getUserId()+"'");
            if(AssertValid.isNotEmpty(maps)) {
                taskConfResult.setUser(maps.get(0));
            } else {
                taskConfResult.setUser(null);
            }
        }
        return taskConfResult;
    }

//    @Override
//    public void updateActRuEmpl(ActionBody argActionBody) {
//        SystemUser systemUser = SystemUserUtil.getSystemUser();
//        // 第一步，拿到当前任务的定义的KEY
//        TaskQuery taskQuery = taskService.createTaskQuery().taskId(argActionBody.getTaskId());
//        String taskDefinitionKey = taskQuery.list().get(0).getTaskDefinitionKey();
//        // 第二步，判断运行时的任务范围表中的任务配置情况
//        LambdaQueryWrapper<TActRuTaskScopeEntity> query = new LambdaQueryWrapper<TActRuTaskScopeEntity>();
//        query.eq(TActRuTaskScopeEntity::getTaskDefKey,taskDefinitionKey);
//        query.eq(TActRuTaskScopeEntity::getActMatterInstId,argActionBody.getMatterInstId());
//        List<TActRuTaskScopeEntity> enList = tActRuTaskScopeMapper.selectList(query);
//        if(enList.size() == 1) {
//            // 如果.getOptId()为空的情况下说明没配置当前任务节点的执行人，在没配置的情况下只能是任务提交人自己能看到待办。
//            // 自己能看到待办的情况下更新运行时任务范围数据，用于记录当前任务节点的处理人，在做回退操作的时候可以跟据当时的处理人进行回退。
//            if(AssertValid.isEmpty(enList.get(0).getOptId())) {
//                TActRuTaskScopeEntity tActRuTaskScopeEntity = enList.get(0);
//                tActRuTaskScopeEntity.setOptId(systemUser.getId());
//                tActRuTaskScopeEntity.setTaskScopeType(2);
//                tActRuTaskScopeMapper.updateById(tActRuTaskScopeEntity);
//            }
//        }
//    }

}